package com.gcsoftware.web.controller.admin;

import com.gcsoftware.configs.MailConfigs;
import com.gcsoftware.utils.ReadFileUtils;
import com.gcsoftware.utils.mail.MailUtils;
import com.gcsoftware.web.service.T_COM_SYS_USER_SERVICE;
import com.gcsoftware.web.beans.T_COM_SYS_USER;
import com.gcsoftware.utils.MapUtils;
import com.gcsoftware.utils.SysConst;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qs on 2018/3/22.
 */
@Controller
@RequestMapping("/users")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private T_COM_SYS_USER_SERVICE service;

    @Autowired
    private MailConfigs mailConfigs;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public List<T_COM_SYS_USER> queryList(@RequestParam(value = "c_USER_NAME" ,required = false) String c_USER_NAME,@RequestParam(value = "c_COMPANY_ID" ,required = false) String c_COMPANY_ID,
                                          @RequestParam(value = "c_USER_STATUS" ,required = false) String c_STATUS) {
        return service.queryForList(c_USER_NAME,c_STATUS,c_COMPANY_ID);
    }

    @RequestMapping(value = "/save" , method =  RequestMethod.POST)
    @ResponseBody
    public MapUtils save(T_COM_SYS_USER user) {
        if (StringUtils.isBlank(user.getC_LOGIN_NAME())) {
            return MapUtils.error("账号名称不能为空！");
        }
        if (StringUtils.isBlank(user.getC_USER_EMAIL())) {
            return MapUtils.error("邮箱不能为空");
        }
        if(StringUtils.isBlank(user.getC_ROLE_ID())) {
            return MapUtils.error("角色不能为空");
        }
        if(StringUtils.isBlank(user.getC_COMPANY_ID())) {
            return MapUtils.error("所属厂家不能为空");
        }
        if(StringUtils.isNotBlank(user.getC_USER_ID())) {
            return service.update(user);
        } else {
            user.setC_STATUS(SysConst.DICT_USER_STATUS_NORMAL);
            if (0 == (Integer) service.insert(user).get("code")) {
                Map pwdMap = new HashMap();
                pwdMap.putAll(service.insertUserPwd(user.getC_USER_ID(),null));

                if (0 == (Integer)pwdMap.get("code")) {
                    try {
                        MailUtils.sendMail(mailConfigs.getMailSendFrom(),new String[]{user.getC_USER_EMAIL()},null,mailConfigs.getSubjectUserPwd(),"你好，欢迎加入数据安全解决方案系统!<br/>系统管理员已为您创建登录账号，请使用以下账号和密码登录系统，并修改密码<br/>--------------------------------------<br/>登录账号：" +user.getC_LOGIN_NAME() +"<br/>登录密码：" + pwdMap.get("originalPwd") + "<br/>--------------------------------------<br/>",true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return MapUtils.ok();
                } else {
                    service.delete(user.getC_USER_ID());
                    return MapUtils.error("新增账户失败");
                }
               /* try {
                    String content = ReadFileUtils.getBody(ReadFileUtils.readFile(ReadFileUtils.getAbsolutePath("/config/mail-temps/" + mailConfigs.getAccountPwdTemp())));
                    content = content.replace("{{0}}",user.getC_USER_ID()).replace("{{1}}",user.getC_LOGIN_NAME());
                    MailUtils.sendMail(mailConfigs.getMailSendFrom(),new String[]{user.getC_USER_EMAIL()}, null,mailConfigs.getSubjectUserPwd(),content,true);
                } catch (Exception e) {
                    e.printStackTrace();
                }*/
                //return MapUtils.ok();
            }
            return MapUtils.error("新增账户失败");
        }
    }

    @RequestMapping(value = "/{c_USER_ID}/delete" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils remove(@PathVariable("c_USER_ID") String c_USER_ID) {
        return service.delete(c_USER_ID);
    }
    @RequestMapping(value = "/{c_USER_ID}/lock" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils lock(@PathVariable("c_USER_ID") String c_USER_ID) {
        return 1 == service.updateUserStatus(c_USER_ID, SysConst.DICT_USER_STATUS_LOCKED) ? MapUtils.ok() : MapUtils.error("锁定失败");
    }

    @RequestMapping(value = "/{c_USER_ID}/unlock" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils unlock(@PathVariable("c_USER_ID") String c_USER_ID) {
        return 1 == service.updateUserStatus(c_USER_ID, SysConst.DICT_USER_STATUS_NORMAL) ? MapUtils.ok() : MapUtils.error("解锁失败");
    }

    @RequestMapping(value = "/{c_USER_ID}/unable" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils unable(@PathVariable("c_USER_ID") String c_USER_ID) {
        return 1 == service.updateUserStatus(c_USER_ID, SysConst.DICT_USER_STATUS_UNABLE) ? MapUtils.ok() : MapUtils.error("禁用失败");
    }

    @RequestMapping(value = "/{c_USER_ID}/updatePwd" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils updatePwd(@PathVariable("c_USER_ID") String c_USER_ID,
                              @RequestParam(value = "c_USER_PWD" , required = true)String c_USER_PWD) {
        return service.insertUserPwd(c_USER_ID,c_USER_PWD);
    }
}
